<?php
class contIndex extends contComm
{
    protected $template = 'sample1';
    
    // http://도메인/index.php/index/test
    public function test()
    {
        $sName = 'test';
        $iOldId = '1';
        $iGenreNo = $this->setGenre($sName, $iOldId);
        
        /*
        $aRow = $this->model('CollectDaum')->getGenre();
        
        var_dump($aRow);
        
        echo 'test';
        exit();
        */
        
        $aParam = array(
            'method' => 'post',
            'url' => 'http://webtoon.daum.net/data/webtoon/list_by_option?genre=&category=&highScore=&minScore=&webtoonStatus=&hotIssue=&page_no=2&page_size=30&timestamp=1401682011130',
            'timeout' => 60
        );
        $aResult = utilCurl::getInstance()->init($aParam)->exec();
        $aData = json_decode($aResult['body'], true);
        
        if ($aData['result']['status'] == 200) {
            
        }
    }
    
    public function dataConvert($aData)
    {
        // 장르
        $aGenre = array();
        
        $aData = $aData['webtoonList'];
        for ($i = 0, $iLen = count($aData); $i < $iLen; $i++) {
            $_row = $aData[$i];
            
            // 정보
            $aWebtoon = array(
                'old_id' => $_row['id'],
                'id' => $_row['nickname'],
                'name' => $_row['title'],
                'media' => $_row['media'],
                'is_finish' => $_row['finishYn'],
                'img' => $_row['padThumbnailImage']['url'],
                'introduction' => $_row['introduction'],
                'start_dt' => $_row['startDate'],
                'service_status' => $_row['serviceStatus'],
                'pay_yn' => $_row['payYn'],
                'price' => $_row['price'],
                'age_grade' => $_row['ageGrade'],
                'average_score' => $_row['averageScore']
            );
            
            $iWebtoonNo = $this->setWebtoon($aWebtoon);
            if (empty($iWebtoonNo)) {
                echo 'webtoon insert error';
                exit;
            }
            
            // 장르
            if (empty($_row['cartoon']['genres']) === false) {
                foreach ($_row['cartoon']['genres'] as $_aVal) {
                    $iGenreNo = $this->setGenre($_aVal['name'], $_aVal['id']);
                    
                    if (empty($iGenreNo)) {
                        echo 'genres insert error : '.print_r($_aVal);
                        exit;
                    }
                    
                    $aGenreNos[] = $iGenreNo;
                    
                    /*
                    $aGenre[] = array(
                        'name' => $_aVal['name'],
                        'old_id' => $_aVal['id']
                    );
                    */
                }
            }
            
            // categories
            if (empty($_row['cartoon']['categories']) === false) {
                foreach ($_row['cartoon']['categories'] as $_aVal) {
                    
                    $iCategoryNo = $this->setCategory($_aVal['name'], $_aVal['categoryType'], $_aVal['id']);
                    if (empty($iCategoryNo)) {
                        echo 'category insert error : '.print_r($_aVal);
                        exit;
                    }
                    
                    $aCategoryNos[] = $iCategoryNo;
                    
                    /*
                    $aGenre[] = array(
                        'name' => $_aVal['name'],
                        'category_type' => $_aVal['categoryType'],
                        'old_id' => $_aVal['id']
                    );
                    */
                }
            }
            
            // webtoonWeeks
            if (empty($_row['webtoonWeeks']) === false) {
                foreach ($_row['webtoonWeeks'] as $_aVal) {
                    $aWebtoonWeeks[] = array(
                        'week_day' => $_aVal['web'],
                        'old_id' => $_aVal['id']
                    );
                }
            }
            
            // artists
            if (empty($_row['cartoon']['artists']) === false) {
                foreach ($_row['cartoon']['artists'] as $_aVal) {
                    $aGenre[] = array(
                        'name' => $_aVal['name'],
                        'pen_name' => $_aVal['penName'],
                        'introduction' => $_aVal['introduction'],
                        'email' => $_aVal['email'],
                        'homepage' => $_aVal['homepage'],
                        'blog' => $_aVal['blog'],
                        'cafe' => $_aVal['cafe'],
                        'fancafe' => $_aVal['fancafe'],
                        'facebook' => $_aVal['facebook'],
                        'twitter' => $_aVal['twitter'],
                        'artist_type' => $_aVal['artistType'],
                        'old_id' => $_aVal['id']
                    );
                }
            }
        }
    }
    
    public function setGenre($sName, $iOldId)
    {
        if (empty($sName) || empty($iOldId)) return false;
        
        return $this->model('CollectDaum')->setGenre($sName, $iOldId);
    }
    
    public function setWebtoon($aData)
    {
        if (empty($aData)) return false;
        
        return $this->model('CollectDaum')->setWebtoon($aData);
    }
    
    public function setCategory($sName, $sType, $iOldId)
    {
        if (empty($sName) || empty($sType) || empty($iOldId)) return false;
        
        return $this->model('CollectDaum')->setCategory($sName, $sType, $iOldId);
    }

    public function index()
    {
        Ui(':uipackBootstrap')->plugin('Validator', array(
            'form' => 'login_form'
        ));
        
        $this->js('default');
        
        if (s()->get('user_id') !== false) {
            $this->setCustom(array(
                //'logged_msg' => '<li><a href="/index.php?controller=index&method=logout" style="color:#F30">LOGOUT</a></li>'
                'logged_msg' => '<li><a href="/index.php/index/logout" style="color:#F30">LOGOUT</a></li>'
            ));
        }
        
        $aRow = $this->model('Example')->get();
        
        $this->view('main', array(
            'article_list' => $aRow
        ));
    }

    public function login()
    {
        $this->setTemplate(null);
        
        $sId = r()->getParam('user_id', 'post');
        $sPass = r()->getParam('user_passwd', 'post');
        
        if ($this->model('Example')->isLogin($sId, $sPass) === true) {
            s()->set('user_id', $sId);
            echo utilJavascript::getInstance()->locationReplace('/index.php');
        } else {
            echo utilJavascript::getInstance()->historyBack('로그인 정보가 올바르지 않습니다.');
        }
    }

    public function logout()
    {
        $this->setTemplate(null);
        
        s()->set('user_id');
        
        echo utilJavascript::getInstance()->locationReplace('/index.php');
    }

    public function join()
    {
        $aParam = r()->getParam();
        
        echo '<pre>';
        print_r($aParam);
        exit();
    }
}